﻿using MesGLApp.DAL.RoleDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MesGLApp.View
{
    partial class MenuLayoutFrm : Smobiler.Core.Controls.MobileUserControl
    {
        private string _title_name = "";

        public MenuLayoutFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        public MenuLayoutFrm(string titleName) : base()
        {
            InitializeComponent();
            if (titleName != null && titleName.Trim().Length > 0)
            {
                this._title_name = titleName;
                lblTitle.Text = titleName;
                this.InitMenuLauout(LoginFrm.MenuList);
            }
        }

        private void InitMenuLauout(List<BaseMenuDTO> menuList)
        {
            if (menuList != null && menuList.Count > 0)
            {
                var query = menuList.Where(m => m.menu_name == this._title_name);
                if (query != null && query.Count() > 0)
                {
                    BaseMenuDTO parMenu = query.First<BaseMenuDTO>();
                    if (parMenu != null && parMenu.ChildMenu != null && parMenu.ChildMenu.Count > 0)
                    {
                        foreach (BaseMenuDTO childMenu in parMenu.ChildMenu)
                        {
                            if (childMenu != null)
                            {
                                IconMenuViewGroup group = new IconMenuViewGroup();
                                group.Text = childMenu.menu_name;
                                group.ShowTitle = true;
                                group.ItemHeight = 0;

                                if (childMenu.ChildMenu != null && childMenu.ChildMenu.Count > 0)
                                {
                                    foreach (BaseMenuDTO menuItem in childMenu.ChildMenu)
                                    {
                                        IconMenuViewItem item = new IconMenuViewItem();
                                        item.Text = menuItem.menu_name;
                                        item.Icon = menuItem.des;
                                        item.Value = menuItem.class_name;
                                        group.Items.Add(item);
                                    }
                                }

                                this.iconMenu.Groups.Add(group);
                            }
                        }
                    }
                }
            }
        }

        private void iconMenu_ItemPress(object sender, IconMenuViewItemPressEventArgs e)
        {
            if (e.Item != null && e.Item.Value != null && e.Item.Value.ToString().Trim().Length > 0)
            {
                Type type = Type.GetType(e.Item.Value.ToString());
                if (type != null)
                {
                    Smobiler.Core.Controls.MobileForm inst = Activator.CreateInstance(type) as Smobiler.Core.Controls.MobileForm;
                    this.Form.Show(inst);
                }
                else 
                {
                    Toast("没有此类，打开失败 ！");
                }
            }
        }
    }
}